#ifndef _RECORD_DATA_IO_H_
#define _RECORD_DATA_IO_H_

#include "scsi_dev.h"
#include "fifo_buf.h"
#include "rec_read_base.h"
#include "rec_write_asf.h"
#include <map>
#include <vector>
using namespace std;

typedef map<uint64_t, CRecWriteBase*> MAP_RECORD_WRITER;

typedef map<uint64_t,  CRecReadBase*> MAP_RECORD_READER;

class  CDiskRecordInfo
{
public:;
	CDiskRecordInfo();
	int RecordFlushProc();
public:
	CDisk_Dev * dev;			 //对应的磁盘设备
	char        storage_name[256];//存储设备名称路径'
	cross_thread_t thd_id;	     //线程ID
	bool        thd_start;		 //线程开启
	bool        wait;
	int         replay_num;		 //回放的文件个数
	uint64_t	full_lba;		 //当前缓冲已经满了的LBA
	MAP_RECORD_WRITER mp_rec;;   //录像文件表
	MAP_RECORD_READER mp_recRead;//
	uchar_t *	rpBuf;			 //回放补齐专用Buf
	uint32_t	rpBufSize;		 //回放补齐专用Buf
	CCross_Lock	devRecLock;
	CCross_Lock devReplayLock;
};

typedef vector<CDiskRecordInfo*>  VEC_DISK_REC_INFO; //设备列表
class CRecordDataIO
{
protected:

	VEC_DISK_REC_INFO m_vec_task;
public:
	static uint32_t ST_WT_WATER;
	static uint32_t ST_RD_WATER;
public:
	int AddRecordTask(char* pStorage,uint64_t lba,CRecWriteBase* pRec);		//增加一个录像任务
	int DelRecordTask(char* pStorage,uint64_t lba,CRecWriteBase* pRec);		//删除录像任务

	int AddReaderTask(char* pStorage,uint64_t & lba,CRecReadBase* pRec);
	int DelReaderTask(char* pStorage,uint64_t lba,CRecReadBase* pRec);
	//强制结束
	int EndTask(char* pStorage);
	//判断刷新任务是否在线
	int IsTaskLive(char * pStorage);

	static int PUB_THREAD_CALL DataFlushThread(void *pParam);
	cross_thread_t GetThreadID(char* pStorage)
	{
		for(int i = 0 ; i < (int)m_vec_task.size() ; i++)
		{
			if(strstr(pStorage,m_vec_task[i]->storage_name))
			{
				return m_vec_task[i]->thd_id;
			}
		}
		return (cross_thread_t)0;
	}
	void SetPlayNum(char* pStorage,int count)
	{
		for(int i = 0; i<(int)m_vec_task.size() ;i++)
		{
			if(strstr(pStorage,m_vec_task[i]->storage_name))
			{
				m_vec_task[i]->replay_num += count;	//count 可以为负数
				if(m_vec_task[i]->replay_num < 0)
				{
					m_vec_task[i]->replay_num = 0;
				}
				return;
			}
		}
	}

	MAP_RECORD_WRITER * GetRecMP(char* pStorage)
	{
		for(int i = 0 ; i < (int)m_vec_task.size() ; i++)
		{
			if(strstr(pStorage,m_vec_task[i]->storage_name))
			{
				return &m_vec_task[i]->mp_rec;
			}
		}
		return NULL;
	}

	MAP_RECORD_READER * GetReplayMP(char* pStorage)
	{
		for(int i = 0 ; i < (int)m_vec_task.size() ; i++)
		{
			if(strstr(pStorage,m_vec_task[i]->storage_name))
			{
				return &m_vec_task[i]->mp_recRead;
			}
		}
		return NULL;
	}

	uchar_t * GetRpBuf(char* pStorage)
	{
		for(int i = 0 ; i < (int)m_vec_task.size() ; i++)
		{
			int bufSz = CStorCacheCfg::getSingleton()->GetMoveDataBufSz();
			if(strstr(pStorage,m_vec_task[i]->storage_name))
			{
				if( m_vec_task[i]->rpBuf == NULL)
				{
					m_vec_task[i]->rpBuf  = new uchar_t[bufSz];
					m_vec_task[i]->rpBufSize = bufSz;
				}
				return m_vec_task[i]->rpBuf;
			}
		}
		return NULL;
	}

	int  GetRpBufSize(char* pStorage)
	{
		for(int i = 0 ; i < (int)m_vec_task.size() ; i++)
		{
			if(strstr(pStorage,m_vec_task[i]->storage_name))
			{
				return m_vec_task[i]->rpBufSize;
			}
		}
		return SIZE_512KB;
	}

	int  ResizeRpBuf(CDisk_Dev* pDev,int size);
};
extern CRecordDataIO g_rec_dataIO;
#endif //_RECORD_TASK_H




